package nl.weeaboo.sh.objects;

import java.awt.Rectangle;

import javax.media.opengl.GL;

import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.model.DamageRect;

public abstract class Sprite {

	public static final Rectangle  noRects[]       = new Rectangle[0];
	public static final DamageRect noDamageRects[] = new DamageRect[0];
	
	private boolean destroyed;
	
	protected Game game;
	protected Level level;
		
	private float power;

	protected int hp;
	protected int maxHp;
	protected int invincibilityTime;
	protected float knockbackInertia;
	protected float rotation;
	protected int x;
	protected int y;
	
	public Sprite(Game game, Level level) {
		this.game = game;
		this.level = level;
		
		maxHp = hp = 1;
		knockbackInertia = 1f;
		setPower(1f);
	}
	
	//Functions
	public void dispose() {
		level.removeSprite(this);		
	}	
	public void destroy() {
		destroyed = true;
	}
	
	public boolean damage(int d) {
		if (!isInvincible()) {
			setHp(hp - d);
			return true;
		}
		return false;
	}
	
	public void update(UserInput input, float dt) {
	}
		
	public void draw(GLManager glm) {
		GL gl = glm.getGL();
		gl.glPushMatrix();
		
		gl.glTranslated(x, y, 0);		
		gl.glRotatef(rotation, 0, 0, -1);
		
		drawImage(glm);
		
		gl.glPopMatrix();
	}
	public void drawForeground(GLManager glm) {
		
	}
	protected abstract void drawImage(GLManager glm);
	
	public void onHitConnect(Sprite damaged, Rectangle hitArea, DamageRect dr) {		
	}
	public boolean onHit(Sprite hitter, int damage, Rectangle hitArea, DamageRect dr) {
		return damage(damage);
	}
	protected void onHitWall(int dir, boolean wallJumpable) {		
	}
	protected void onHitCeiling() {
	}
	protected void onLand() {
	}
	protected void onStomped(Sprite stomper) {		
	}
	
	//Getters
	public final boolean isDestroyed() { return destroyed; }

	public Level getLevel() { return level; }
	public int getHp() { return hp; }
	public int getMaxHp() { return maxHp; }
	public float getPower() { return power; }
	public boolean isInvincible() { return invincibilityTime > 0; }
	
	public int getX() { return x; }
	public int getY() { return y; }
	public double getRotation() { return rotation; }
	
	public abstract Rectangle[]  getHitRects();
	public abstract DamageRect[] getDamageRects();
	public abstract DamageRect[] getSplashRects();
		
	//Setters
	public void setHp(int i) {
		if (hp <= maxHp) {
			hp = Math.max(0, i);
			if (hp <= 0) {
				destroy();
			}
		}
	}
	public void setMaxHp(int hp) {
		maxHp = hp;
		this.hp = Math.min(maxHp, getHp());
	}
	public void setPower(float p) {
		power = p;
	}
	public void setRotation(float r) {
		rotation = r;
	}
	public void setXY(int x, int y) {
		setX(x);
		setY(y);
	}
	public void setX(int x) {
		this.x = x;
	}
	public void setY(int y) {
		this.y = y;
	}
	public void setInvincibilityTime(int t) {
		invincibilityTime = t;
	}
	
}
